package com.woniu.common.config;


import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;

@Configuration
public class PostQueueConfig {

    public static final String POST_LIKE_EXCHANGE = "post_like_exchange";
    public static final String ALQ_POST_EXCHANGE = "dlq_post_exchange";
    public static final String ALQ_POST_QUEUE = "dlq_post_queue";
    public static final String POST_LIKE_QUEUE = "post_like_queue";
    public static final String POST_LIKE_ROUTING_KEY = "post_like_timeout";
    public static final String ROUNT_KEY = "dlq";
    public static final Long X_MESSAGE_TTL = 60 * 1000L;
    public static final Integer MAX_LENGTH = 3;

    //创建交换机
    @Bean
    public Exchange confirmExchange(){
        //使用构建者方式创建
        return ExchangeBuilder.directExchange(POST_LIKE_EXCHANGE).durable(true).build();
    }

//    //死信交换机
//    @Bean
//    public Exchange dlqExchange(){
//        return ExchangeBuilder.directExchange(ALQ_POST_EXCHANGE).durable(true).build();
//    }
//
//    //死信队列
//    @Bean
//    public Queue dlqPostQueue(){
//        return QueueBuilder.durable(ALQ_POST_QUEUE).build();
//    }

    //创建队列
    @Bean
    public Queue confirmQueue(){
        HashMap<String, Object> params = new HashMap<>();
        // 设置过期时间
        params.put("x-message-ttl", X_MESSAGE_TTL);
        // 设置死信交换机
//        params.put("x-dead-letter-exchange", ALQ_POST_EXCHANGE);
        // 设置路由key
//        params.put("x-dead-letter-routing-key", ROUNT_KEY);
//        // 设置队列最大长度
//        params.put("x-max-length", MAX_LENGTH);

        return QueueBuilder.durable(POST_LIKE_QUEUE).withArguments(params).build();
    }

    // 将死信队列绑定到死信交换机上
//    @Bean
//    public Binding dlqBinding(@Qualifier("dlq_post_queue") Queue queue, @Qualifier("dlq_post_exchange") Exchange exchange){
//        return BindingBuilder.bind(queue).to(exchange).with(ROUNT_KEY).noargs();
//    }

    @Bean
    public Binding confirmBinding(@Qualifier("confirmQueue") Queue queue, @Qualifier("confirmExchange") Exchange exchange){
        //从容器拿对象
        return BindingBuilder.bind(queue).to(exchange).with(POST_LIKE_ROUTING_KEY).noargs();
    }
}
